﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Save
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////

using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Drawing;

namespace Map_Fvg
{
    class Save
    {
        public static int modificaTesto = 0;
        public static int modificaLinea = 0;
        public static int modificaPunto = 0;
        public static int modificaArea = 0; 

        public Save()
        {


        }

        public void SaveFile(Form this1)
        {
            string path = string.Concat(FormIniz.cartellaElementi, FormIniz.nameElement);
            if (FormIniz.bVeneto) path = string.Concat(FormIniz.cartellaElementi_isolati, FormIniz.nameElement);
            SaveFileDialog textDialog = new SaveFileDialog();
            textDialog.Filter = "Zip Files | *.zip";
            textDialog.DefaultExt = "zip";
            textDialog.FileName = path;  // ViewQ.selectedElement;
            textDialog.InitialDirectory = FormIniz.cartellaElementi;
            if (DialogResult.OK == textDialog.ShowDialog())
            {
                int iret = 0;
                int exec = 0;   
                string fileNameX = textDialog.FileName;
                if (!fileNameX.Contains(".zip"))
                {
                    MessageBox.Show(fileNameX + "   " + "nome non valido");
                    goto L90;
                }

                this1.Cursor = Cursors.WaitCursor;

                if (modificaLinea != 0)
                {
                    iret = salvaLinea();
                    if (iret != 0) goto L90;
                    modificaLinea = 0;
                    ++exec;
                }

                if (modificaTesto != 0)
                {
                    iret = salvaTesto();
                    if (iret != 0) goto L90;
                    modificaTesto = 0;
                    ++exec;
                }

                if (modificaPunto != 0)
                {
                    iret = salvaPunto();
                    if (iret != 0) goto L90;
                    modificaPunto = 0;
                    ++exec;
                }

                if (modificaArea != 0)
                {
                    iret = salvaArea();
                    if (iret != 0) goto L90;
                    modificaArea = 0;
                    ++exec;
                }
                if (!File.Exists(fileNameX)) ++exec;
                if (exec > 0)
                {
                    Zip.Extract1 Extr = new Zip.Extract1();
                    //                           Zip.Trace.Open("Zipfile1.txt");
                    string path1 = FormIniz.quadroUnione5000;
                    iret = Extr.CompressToFile(path1, fileNameX);
                }
            L90:
                this1.Cursor = Cursors.Default;
                if (iret != 0)
                {
                    MessageBox.Show("impossibile eseguire il salvataggio");
                    return;
                }

                if (exec > 0) MessageBox.Show("salvataggio completato");

            }
        }

        private int salvaTesto()
        {
            string file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1,"text_shp.txt");

  //          bool ex = File.Exists(file1);

            string file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2,"text_shpA.txt");

      //      File.Create(file2);

    //        ex = File.Exists(file2);

            int idel = 0;
            int imod = 0;
            int inew = 0;

            int nrec = ViewQ.nShapesT;
            for (int i = 0; i < nrec; ++i)
            {
                if (ViewQ.vectShapesT[i].modifica == 0) continue;
                if (ViewQ.vectShapesT[i].modifica == 1) ++imod;
                if (ViewQ.vectShapesT[i].modifica == 2) ++idel;
                if (ViewQ.vectShapesT[i].modifica == 3) ++inew;
            }

            StreamReader sf = File.OpenText(file1);
            string s1 = sf.ReadLine();

            if (!s1.Contains("Shapefile type: MultiPointZ"))
            {
                MessageBox.Show("file text_shp.txt  non conforme");
                return 1;
            }

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
            char[] delimiterChars0 = { ':', ';' };
            string[] words;

            words = s1.Split(delimiterChars0);

            string ss = words[2];
            int nrec1 = int.Parse(ss);
            if (nrec != nrec1 + inew)
            {
                MessageBox.Show("numero record non idoneo: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString());
                return 1;

            }
            int nrec2 = nrec - idel ; 
            StreamWriter sw = new StreamWriter(file2);
            s1 = "Shapefile type: MultiPointZ  # of Shape: ";
            s1 = string.Concat(s1, nrec2.ToString() + "; # of Points: 0");
            sw.WriteLine(s1);

            s1 = sf.ReadLine();
            sw.WriteLine(s1);

            int j = 0;
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapesT[i].modifica;
                int XX = ViewQ.vectShapesT[i].XX.X;
                int YY = ViewQ.vectShapesT[i].XX.Y;
                if (opt == 0)
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(),", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    sw.WriteLine(s1);
                    ++j;
                }
                if (opt == 1)
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(), ", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, XX.ToString(), " , ", YY.ToString(), " , ", XX.ToString(), " , ", YY.ToString());
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    s1 = XX.ToString();
                    s1 = string.Concat(s1, " , ", YY.ToString());
                    sw.WriteLine(s1);
                    ++j;
                }
                if (opt == 2)
                {
                    s1 = sf.ReadLine();
                    s1 = sf.ReadLine();
                    s1 = sf.ReadLine();
                }
                if (opt == 3)
                {
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(), ", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, XX.ToString(), " , ", YY.ToString(), " , ", XX.ToString(), " , ", YY.ToString());
                    sw.WriteLine(s1);
                    s1 = XX.ToString();
                    s1 = string.Concat(s1, " , ", YY.ToString());
                    sw.WriteLine(s1);
                    ++j;
                }
            }
            sf.Close();
            sw.Close();

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "text_dbf.txt");

            //          bool ex = File.Exists(file1);

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "text_dbfA.txt");

            StreamReader sf1 = File.OpenText(file1);
            s1 = sf1.ReadLine();

            if (!s1.Contains("numero records:"))
            {
                MessageBox.Show("file text_dbf.txt  non conforme");
                return 1;
            }
            words = s1.Split(delimiterChars0);

            ss = words[1];
            //           MessageBox.Show("numero records: " + ss);
            nrec1 = int.Parse(ss);
            if (nrec != nrec1 + inew)
            {
                MessageBox.Show("numero record non idoneo: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString());
                return 1;

            }

            StreamWriter sw1 = new StreamWriter(file2);
            s1 = "numero records:   ";
            s1 = string.Concat(s1, nrec2.ToString());
            sw1.WriteLine(s1);
            string s2 = string.Empty;
            string s3 = string.Empty;
            j = 0;

            char[] delimiterChars1 = { '#' };
            string blanks = "                                                                                ";
            words = s1.Split(delimiterChars0);
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapesT[i].modifica;
                if (modificaTesto == 2) opt = 1;
                if(opt < 3) s1 = sf1.ReadLine();
                s3 = "    ";
                if (j > 9) s3 = "   ";
                if (j > 99) s3 = "  ";
                if (j > 999) s3 = " ";

                if (opt == 0)
                {
                    int index= s1.IndexOf('#');
                    int length = s1.Length;
                    s2 = s1.Substring(index,length - index);
                    s3 = string.Concat(s3,j.ToString(), s2);
                    sw1.WriteLine(s3);
                    ++j;
                }
                if (opt == 1|| opt == 3)
                {
                    words = s1.Split(delimiterChars1);
                    int pntSimb = ViewQ.vectShapesT[i].pntSimb;
                    Font ifont = ViewQ.vectT[pntSimb].iFont;
                    float angle = ViewQ.vectShapesT[i].GMrotation;

                    string codice = ViewQ.vectT[pntSimb].codice;
                    int length = codice.Length;
                    if(length < 10) codice = string.Concat(codice,blanks.Substring(0,10-length));
                    s3 = string.Concat(s3,j.ToString(), "#",codice,"#");

                    string denom = ViewQ.vectT[pntSimb].denom;
                    length = denom.Length;
                    if (length < 80) denom = string.Concat(denom, blanks.Substring(0, 80 - length));
                    s3 = string.Concat(s3,denom, "#");

                    string classe = ViewQ.vectT[pntSimb].classe;
                    length = classe.Length;
                    if (length < 20) classe = string.Concat(classe, blanks.Substring(0, 20 - length));
                    s3 = string.Concat(s3,classe, "#");

                    string elemento = ViewQ.vectT[pntSimb].elemento;
                    length = elemento.Length;
                    if (length < 8) elemento = string.Concat(elemento, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, elemento, "#");

                    int edizione = ViewQ.vectT[pntSimb].edizione;
                    string ediz = edizione.ToString();
                    length = ediz.Length;
                    if (length < 8) ediz = string.Concat(ediz, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, ediz, "#");

                    string testo = ViewQ.vectShapesT[i].testo;
                    length = testo.Length;
                    if (length < 50) testo = string.Concat(testo, blanks.Substring(0, 50 - length));
                    s3 = string.Concat(s3, testo, "#");

                    float altezza = ViewQ.vectShapesT[i].altezza;
                    testo = altezza.ToString("F2",nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length),testo);
                    s3 = string.Concat(s3, testo, "#");

                    float rotazione = ViewQ.vectShapesT[i].rotazione;
                    testo = rotazione.ToString("F2",nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    int ncar_testo = ViewQ.vectShapesT[i].ncar_testo;
                    testo = ncar_testo.ToString();
                    length = testo.Length;
                    if (length < 5) testo = string.Concat(blanks.Substring(0, 5 - length),testo);
                    s3 = string.Concat(s3, testo, "#");

                    int cod_posiz = ViewQ.vectShapesT[i].cod_posiz;
                    testo = cod_posiz.ToString();
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 5 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    float GMrotazione = ViewQ.vectShapesT[i].GMrotation;
                    testo = GMrotazione.ToString("F3",nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    sw1.WriteLine(s3);
                    ++j;
                }
                if (opt == 2)
                {
                }
            }
  
            sf1.Close();
            sw1.Close();

            File.Delete(file1);
            File.Move(file2,file1);

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1,"text_shp.txt");

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2,"text_shpA.txt");

            File.Delete(file1);
            File.Move(file2, file1);

            nrec = ViewQ.nShapesT;
            for (int i = 0; i < nrec; ++i) ViewQ.vectShapesT[i].modifica = 0;

            return 0;
        }
//
//*******************
//
        private int salvaLinea()
        {
            string file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "line_shp.txt");

            string file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "line_shpA.txt");

            int idel = 0;
            int imod = 0;
            int inew = 0;
            int inew_del = 0;

            int nrec = ViewQ.nShapes;
            for (int i = 0; i < nrec; ++i)
            {
                if (ViewQ.vectShapes[i].modifica == 0) continue;
                if (ViewQ.vectShapes[i].modifica == 1) ++imod;
                if (ViewQ.vectShapes[i].modifica == 2) ++idel;
                if (ViewQ.vectShapes[i].modifica == 3) ++inew;
                if (ViewQ.vectShapes[i].modifica == 4) ++imod;  //  nodi aggiunti o cancellati
                if (ViewQ.vectShapes[i].modifica == 5) ++inew_del;  //  linee inserite e poi cancellate

            }

            StreamReader sf = File.OpenText(file1);
            string s1 = sf.ReadLine();

            if (!s1.Contains("Shapefile type: ArcZ"))
            {
                MessageBox.Show("file line_shp.txt  non conforme");
                return 1;
            }

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
            char[] delimiterChars0 = { ':', ';','=' };
            string[] words;

            words = s1.Split(delimiterChars0);

            string ss = words[2];
            int nrec1 = int.Parse(ss);
  //          if (nrec != nrec1 + inew + idel)
            if (nrec != nrec1 + inew + inew_del)
            {
                MessageBox.Show("numero record non idoneo-1: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString() + 
                        " + " + inew_del.ToString());
                return 1;

            }

            int nrec2 = nrec - idel - inew_del; 
                                 
            int nPoints = int.Parse(words[4]);
            int nPoints1 = nPoints;
            if (idel > 0 || inew > 0 || inew_del > 0)
            {
                nPoints1 = 0;
                for(int i = 0; i < nrec; ++i)
                {
                    if (ViewQ.vectShapes[i].modifica == 2) continue;
                    if (ViewQ.vectShapes[i].modifica == 5) continue;
                    nPoints1 += ViewQ.vectShapes[i].nVertices;
                }
            }

            StreamWriter sw = new StreamWriter(file2);
            s1 = "Shapefile type: ArcZ  # of Shape: ";
            s1 = string.Concat(s1, nrec2.ToString() + "; # of Points: ",nPoints1.ToString());      
            sw.WriteLine(s1);

            s1 = sf.ReadLine();
            sw.WriteLine(s1);

            int jj = 0;
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapes[i].modifica;
//
//              linea invariata
//
                jj = ViewQ.vectShapes[i].numID;
                if (opt == 0)               
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    int nVertices = ViewQ.vectShapes[i].nVertices;
                    s1 = string.Concat(s1, jj.ToString(), ", ArcZ nVertices=", nVertices.ToString(), ", nParts=1"); 
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    sw.WriteLine(s1);
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s1 = sf.ReadLine();
                        sw.WriteLine(s1);
                    }
 //                   ++jj;
                }
//
//              linea modificata nel valore dei nodi
//
                if (opt == 1)
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    int nVertices = ViewQ.vectShapes[i].nVertices;

                    int pntl = ViewQ.vectShapes[i].pntpL;
                    int Xmin = ViewQ.PL[pntl].X;
                    int Xmax = ViewQ.PL[pntl].X;
                    int Ymin = ViewQ.PL[pntl].Y;
                    int Ymax = ViewQ.PL[pntl].Y;
                    for (int j = 1; j < nVertices; ++j)
                    {
                        if (Xmin > ViewQ.PL[pntl].X) Xmin = ViewQ.PL[pntl].X;
                        if (Xmax < ViewQ.PL[pntl].X) Xmax = ViewQ.PL[pntl].X;
                        if (Ymin > ViewQ.PL[pntl].Y) Ymin = ViewQ.PL[pntl].Y;
                        if (Ymax < ViewQ.PL[pntl].Y) Ymax = ViewQ.PL[pntl].Y;
                        ++pntl;
                    }
                    ViewQ.vectShapes[i].PointA.X = Xmin;
                    ViewQ.vectShapes[i].PointA.Y = Ymin;
                    ViewQ.vectShapes[i].PointB.X = Xmax;
                    ViewQ.vectShapes[i].PointB.Y = Ymax;
                    pntl = ViewQ.vectShapes[i].pntpL;

                      s1 = string.Concat(s1, jj.ToString(), ", ArcZ nVertices=", nVertices.ToString(), ", nParts=1");
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, Xmin.ToString(), " , ", Ymin.ToString(), " , ", Xmax.ToString(), " , ", Ymax.ToString()); 
                    sw.WriteLine(s1);
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s1 = sf.ReadLine();
                        s1 = "     ";
                        s1 = string.Concat(s1, ViewQ.PL[pntl].X.ToString(), " ,   ", ViewQ.PL[pntl].Y.ToString(), ",");
                        sw.WriteLine(s1);
                        ++pntl;
                    }
 //                   ++jj;
                }
//
//              linea cancellata
//
                if (opt == 2 || opt == 5)
                {
                    int nVertices = ViewQ.vectShapes[i].nVertices;
                    int pntl = ViewQ.vectShapes[i].pntpL;
                    s1 = sf.ReadLine();
                    s1 = sf.ReadLine();
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s1 = sf.ReadLine();
                    }
                }
//
//              aggiunta linea
//
                if (opt == 3)
                {
                    int nVertices = ViewQ.vectShapes[i].nVertices;
                    int Xmin = ViewQ.vectShapes[i].PointA.X;
                    int Ymin = ViewQ.vectShapes[i].PointA.Y;
                    int Xmax = ViewQ.vectShapes[i].PointB.X;
                    int Ymax = ViewQ.vectShapes[i].PointB.Y;
                    int pntl = ViewQ.vectShapes[i].pntpL;

                    s1 = "Shape:";
                    s1 = string.Concat(s1, jj.ToString(), ", ArcZ  nVertices=",nVertices.ToString(),", nParts=1");
                    sw.WriteLine(s1);
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, Xmin.ToString(), " , ", Ymin.ToString(), " , ", Xmax.ToString(), " , ", Ymax.ToString());
                    sw.WriteLine(s1);
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s1 = "     ";
                        s1 = string.Concat(s1, ViewQ.PL[pntl].X.ToString(), " ,   ", ViewQ.PL[pntl].Y.ToString(), ",");
                        sw.WriteLine(s1);
                        ++pntl;
                    }
 //                   ++jj;
                }
//
//              modifica linea con numero diverso di nodi
//
                if (opt == 4)
                {
                    s1 = sf.ReadLine();
                    char[] delimiterChars3 = { ':', ';', '=', ',' };

                    words = s1.Split(delimiterChars3);

                    ss = words[3];

                    int nVertices1 = int.Parse(ss);
    //                MessageBox.Show(ss + "  " + nVertices1);

                    s1 = sf.ReadLine();
                    for (int j = 0; j < nVertices1; ++j)
                    {
                        s1 = sf.ReadLine();
                    }
                    int nVertices = ViewQ.vectShapes[i].nVertices;
                    int pntl = ViewQ.vectShapes[i].pntpL;
                    s1 = "Shape:";

                    int Xmin = ViewQ.PL[pntl].X;
                    int Xmax = ViewQ.PL[pntl].X;
                    int Ymin = ViewQ.PL[pntl].Y;
                    int Ymax = ViewQ.PL[pntl].Y;
                    for (int j = 1; j < nVertices; ++j)
                    {
                        if (Xmin > ViewQ.PL[pntl].X) Xmin = ViewQ.PL[pntl].X;
                        if (Xmax < ViewQ.PL[pntl].X) Xmax = ViewQ.PL[pntl].X;
                        if (Ymin > ViewQ.PL[pntl].Y) Ymin = ViewQ.PL[pntl].Y;
                        if (Ymax < ViewQ.PL[pntl].Y) Ymax = ViewQ.PL[pntl].Y;
                        ++pntl;
                    }
                    ViewQ.vectShapes[i].PointA.X = Xmin;
                    ViewQ.vectShapes[i].PointA.Y = Ymin;
                    ViewQ.vectShapes[i].PointB.X = Xmax;
                    ViewQ.vectShapes[i].PointB.Y = Ymax;
                    pntl = ViewQ.vectShapes[i].pntpL;

                    s1 = string.Concat(s1, jj.ToString(), ", ArcZ nVertices=", nVertices.ToString(), ", nParts=1");
                    sw.WriteLine(s1);
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, Xmin.ToString(), " , ", Ymin.ToString(), " , ", Xmax.ToString(), " , ", Ymax.ToString());
                    sw.WriteLine(s1);
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s1 = "     ";
                        s1 = string.Concat(s1, ViewQ.PL[pntl].X.ToString(), " ,   ", ViewQ.PL[pntl].Y.ToString(), ",");
                        sw.WriteLine(s1);
                        ++pntl;
                    }
 //                   ++jj;
                }
            }

            sf.Close();
            sw.Close();

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "line_dbf.txt");

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "line_dbfA.txt");

            StreamReader sf1 = File.OpenText(file1);
            s1 = sf1.ReadLine();

            if (!s1.Contains("numero records:"))
            {
                MessageBox.Show("file line_dbf.txt  non conforme");
                return 1;
            }
            words = s1.Split(delimiterChars0);

            ss = words[1];
            nrec1 = int.Parse(ss);
            if (nrec != nrec1 + inew + inew_del)
            {
                MessageBox.Show("numero record non idoneo-2: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString() +
                    " + " + inew_del.ToString());
                return 1;

            }
            StreamWriter sw1 = new StreamWriter(file2);
            s1 = "numero records:   ";
            s1 = string.Concat(s1, nrec2.ToString());
            sw1.WriteLine(s1);
            string s2 = string.Empty;
            string s3 = string.Empty;
            jj = 0;

            char[] delimiterChars1 = { '#' };
            string blanks = "                                                                                ";
            words = s1.Split(delimiterChars0);
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapes[i].modifica;
                if (opt < 3) s1 = sf1.ReadLine();
                s3 = "    ";
                jj = ViewQ.vectShapes[i].numID;
                if (jj > 9) s3 = "   ";
                if (jj > 99) s3 = "  ";
                if (jj > 999) s3 = " ";

//              linea invariata

                if (opt == 0)
                {
                    int index = s1.IndexOf('#');
                    int length = s1.Length;
                    s2 = s1.Substring(index, length - index);
                    s3 = string.Concat(s3, jj.ToString(), s2);
                    sw1.WriteLine(s3);
                    ++jj;
                }
//  opt == 1    linea     modificata nel valore dei nodi
//  opt == 3    aggiunta nuova linea
//  opt == 4    linea     modifica linea con numero diverso di nodi

                if (opt == 1 || opt == 3 || opt == 4)
                {
     //               words = s1.Split(delimiterChars1);
                    int pntSimb = ViewQ.vectShapes[i].pntSimb;

                    string codice = ViewQ.vectS[pntSimb].codice;
                    int length = codice.Length;
                    if (length < 10) codice = string.Concat(codice, blanks.Substring(0, 10 - length));
                    s3 = string.Concat(s3, jj.ToString(), "#", codice, "#");
                    string denom = ViewQ.vectS[pntSimb].denom;
                    length = denom.Length;
                    if (length < 80) denom = string.Concat(denom, blanks.Substring(0, 80 - length));
                    s3 = string.Concat(s3, denom, "#");

                    string classe = ViewQ.vectS[pntSimb].classe;
                    length = classe.Length;
                    if (length < 20) classe = string.Concat(classe, blanks.Substring(0, 20 - length));
                    s3 = string.Concat(s3, classe, "#");

                    string elemento = ViewQ.vectS[pntSimb].elemento;
                    length = elemento.Length;
                    if (length < 8) elemento = string.Concat(elemento, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, elemento, "#");

                    string testo = ViewQ.vectShapes[i].edizione;
          if (testo == null) testo = " ";
                    length = testo.Length;
                    if (length < 8) testo = string.Concat(testo, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, testo, "#");

                    testo = ViewQ.vectShapes[i].lunghezza;
           if (testo == null) testo = " ";
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length),testo);
                    s3 = string.Concat(s3, testo, "#");

                    testo = ViewQ.vectShapes[i].quota;
           if (testo == null) testo = " ";
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    sw1.WriteLine(s3);
                }

//              linea cancellata
                if (opt == 2 || opt == 5)
                {
                }
            }
 
            sf1.Close();
            
            sw1.Close();
            File.Delete(file1);
            File.Move(file2, file1);

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "line_shp.txt");

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "line_shpA.txt");

            File.Delete(file1);
            File.Move(file2, file1);

            nrec = ViewQ.nShapes;
            for (int i = 0; i < nrec; ++i) ViewQ.vectShapes[i].modifica = 0;

            return 0;
        }
//
//******************
//

        private int salvaPunto()
        {
            string file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "point_shp.txt");

            string file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "point_shpA.txt");

            int idel = 0;
            int imod = 0;
            int inew = 0;

            int nrec = ViewQ.nShapesP;
            for (int i = 0; i < nrec; ++i)
            {
                if (ViewQ.vectShapesP[i].modifica == 0) continue;
                if (ViewQ.vectShapesP[i].modifica == 1) ++imod;
                if (ViewQ.vectShapesP[i].modifica == 2) ++idel;
                if (ViewQ.vectShapesP[i].modifica == 3) ++inew;
            }

            StreamReader sf = File.OpenText(file1);
            string s1 = sf.ReadLine();

            if (!s1.Contains("Shapefile type: MultiPointZ"))
            {
                MessageBox.Show("file point_shp.txt  non conforme");
                return 1;
            }

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
            char[] delimiterChars0 = { ':', ';' };
            string[] words;

            words = s1.Split(delimiterChars0);

            string ss = words[2];
            int nrec1 = int.Parse(ss);
            if (nrec != nrec1 + inew)
            {
                MessageBox.Show("point_shp: numero record non idoneo: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString());
                return 1;

            }
            int nrec2 = nrec - idel; //+ inew;
            //                      MessageBox.Show("numero records: " + nrec2.ToString());

            StreamWriter sw = new StreamWriter(file2);
            s1 = "Shapefile type: MultiPointZ  # of Shape: ";
            s1 = string.Concat(s1, nrec2.ToString(),"; # of Points: ", nrec2.ToString()); 
            sw.WriteLine(s1);

            s1 = sf.ReadLine();
            sw.WriteLine(s1);           // file bounds    //da verificare se si aggiungono o togono dei punti

            int j = 0;
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapesP[i].modifica;
                int XX = ViewQ.vectShapesP[i].XX.X;
                int YY = ViewQ.vectShapesP[i].XX.Y;

                if (opt == 0)
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(), ", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    sw.WriteLine(s1);
                    ++j;
                }

                if (opt == 1)
                {
                    s1 = sf.ReadLine();
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(), ", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, XX.ToString(), " , ", YY.ToString(), " , ", XX.ToString(), " , ", YY.ToString());
                    sw.WriteLine(s1);
                    s1 = sf.ReadLine();
                    s1 = XX.ToString();
                    s1 = string.Concat(s1, " , ", YY.ToString());
                    sw.WriteLine(s1);
                    ++j;
                }

                if (opt == 2)
                {
                    s1 = sf.ReadLine();
                    s1 = sf.ReadLine();
                    s1 = sf.ReadLine();
                }

                if (opt == 3)
                {
                    s1 = "Shape:";
                    s1 = string.Concat(s1, j.ToString(), ", MultiPointZ  nVertices=1, nParts=0");
                    sw.WriteLine(s1);
                    s1 = "Bounds: ";
                    s1 = string.Concat(s1, XX.ToString(), " , ", YY.ToString(), " , ", XX.ToString(), " , ", YY.ToString());
                    sw.WriteLine(s1);
                    s1 = XX.ToString();
                    s1 = string.Concat(s1, " , ", YY.ToString());
                    sw.WriteLine(s1);
                    ++j;
                }
            }
            sf.Close();
            sw.Close();

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "point_dbf.txt");

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "point_dbfA.txt");

            StreamReader sf1 = File.OpenText(file1);
            s1 = sf1.ReadLine();

            if (!s1.Contains("numero records:"))
            {
                MessageBox.Show("file point_dbf.txt  non conforme");
                return 1;
            }

            words = s1.Split(delimiterChars0);

            ss = words[1];
            //           MessageBox.Show("numero records: " + ss);
            nrec1 = int.Parse(ss);
            if (nrec != nrec1 + inew)
            {
                MessageBox.Show("point_dbf: numero record non idoneo: " + nrec.ToString() + " != " + nrec1.ToString() + " + " + inew.ToString());
                return 1;

            }

            StreamWriter sw1 = new StreamWriter(file2);
            s1 = "numero records:   ";
            s1 = string.Concat(s1, nrec2.ToString());
            sw1.WriteLine(s1);
            string s2 = string.Empty;
            string s3 = string.Empty;
            j = 0;

            char[] delimiterChars1 = { '#' };
            string blanks = "                                                                                ";
            words = s1.Split(delimiterChars0);
            for (int i = 0; i < nrec; ++i)
            {
                int opt = ViewQ.vectShapesP[i].modifica;
                if (modificaPunto == 2) opt = 1;
                if (opt < 3) s1 = sf1.ReadLine();
                s3 = "    ";
                if (j > 9) s3 = "   ";
                if (j > 99) s3 = "  ";
                if (j > 999) s3 = " ";

                if (opt == 0)
                {
                    int index = s1.IndexOf('#');
                    int length = s1.Length;
                    s2 = s1.Substring(index, length - index);
                    ++j;
                    s3 = string.Concat(s3, j.ToString(), s2);
                    sw1.WriteLine(s3);
                }
                if (opt == 1 || opt == 3)
                {
                    words = s1.Split(delimiterChars1);
                    int pntSimb = ViewQ.vectShapesP[i].pntSimb;
        //            Font ifont = ViewQ.vectT[pntSimb].iFont;
        //            float angle = ViewQ.vectShapesT[i].GMrotation;

                    string codice = ViewQ.vectP[pntSimb].codice;
                    int length = codice.Length;
                    if (length < 10) codice = string.Concat(codice, blanks.Substring(0, 10 - length));
                    s3 = string.Concat(s3, j.ToString(), "#", codice, "#");

                    string denom = ViewQ.vectP[pntSimb].denom;
                    length = denom.Length;
                    if (length < 80) denom = string.Concat(denom, blanks.Substring(0, 80 - length));
                    s3 = string.Concat(s3, denom, "#");

                    string classe = ViewQ.vectP[pntSimb].classe;
                    length = classe.Length;
                    if (length < 20) classe = string.Concat(classe, blanks.Substring(0, 20 - length));
                    s3 = string.Concat(s3, classe, "#");

                    string elemento = ViewQ.vectShapesP[i].elemento;
                    length = elemento.Length;
                    if (length < 8) elemento = string.Concat(elemento, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, elemento, "#");

                    int edizione = ViewQ.vectShapesP[i].edizione;
                    string ediz = edizione.ToString();
                    length = ediz.Length;
                    if (length < 8) ediz = string.Concat(ediz, blanks.Substring(0, 8 - length));
                    s3 = string.Concat(s3, ediz, "#");

                    float quota = ViewQ.vectShapesP[i].quota;
                    string testo = quota.ToString("F2", nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    float dimens = ViewQ.vectShapesP[i].dimensione;
                    testo = dimens.ToString("F2", nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    float rotazione = ViewQ.vectShapesP[i].rotazione;
                    testo = rotazione.ToString("F2", nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    float GMrotazione = ViewQ.vectShapesP[i].GMrotation;
                    testo = GMrotazione.ToString("F3", nfi);
                    length = testo.Length;
                    if (length < 10) testo = string.Concat(blanks.Substring(0, 10 - length), testo);
                    s3 = string.Concat(s3, testo, "#");

                    sw1.WriteLine(s3);
                    ++j;
                }

                if (opt == 2)
                {
                }

            }

            sf1.Close();
            sw1.Close();

            File.Delete(file1);
            File.Move(file2, file1);

            file1 = FormIniz.quadroUnione5000;
            file1 = string.Concat(file1, "point_shp.txt");

            file2 = FormIniz.quadroUnione5000;
            file2 = string.Concat(file2, "point_shpA.txt");

            File.Delete(file1);
            File.Move(file2, file1);

            nrec = ViewQ.nShapesP;
            for (int i = 0; i < nrec; ++i) ViewQ.vectShapesP[i].modifica = 0;

            return 0;

        }


         private int salvaArea()
        {

            return 0;
        }



    }
}
